<?php
namespace Admin\Controller;

use Util\AccessController;

class CostController extends AccessController {

    public function Index() {
        Util("LoadPage");
        $this->page = \Util\LoadPage::show();
        $this->search = \Util\LoadPage::search();
        $LS = Service('User', 'u_user');
        $DATA = $LS->query();
        $costDatas = array();
        foreach ($DATA as $k => $v) {
            $csave['USER_ID'] = $v['user_id'];
            $csave['BOOK_B'] = 0;
            $csave['MONEY'] = passport_encrypt('0.00', C('JM_KEY'));
            $csave['UPDATE_TIME'] = date('Y-m-d H:i:s');
            $csave['UPDATE_IP'] = get_client_ip();
            $costDatas[] = $csave;
        }
        $LS1 = Service('M', 'cost');
        $LS1->AddAll($costDatas);
        $this->display();
    }

    public function pageLoad_self() {
        Util("LoadPage");
        $page = new \Util\LoadPage($_POST);
        $limit = $page->limit($_POST['page']);
        $WEHE = LIKEGET_ADMIN($_GET);
        //是否入库
        $_GET['STATE'] ? $WEHE['STATE'] = $_GET['STATE'] : 1;
        $LS = Service('User', 'u_user');
        $order = 'CREATE_TIME desc';
        $DATA = $LS->query($WEHE, $limit, null, $order);
        $count = $LS->count($WEHE);
        echo $page->getRes($DATA, $count);
    }

    /*
     * 查看用户余额
     */
    public function LookUserMoney() {
        if($this->S('LEVEL') >1){
            $LS = Service('M', 'cost');
            $DATA = $LS->find(array('USER_ID' => $_POST['USER_ID']));
            echo passport_decrypt($DATA['MONEY'], C('JM_KEY'));
        }else{
            echo -1;
        }

    }


    /*
     * 超级管理员增减用户余额
     * 添加记录
     */
    public function UpdateCost(){
        if($this->S('USER_NAME') == 'mao'){
            if(is_numeric($_POST['updateCost'])){
                $LS = Service('Cost','cost');
                $OK = $LS->UpdateCost($_POST['USER_ID'],$_POST['updateCost']);
                $OK?$this->success('修改成功'):$this->error('修改失败');
            }else{
                $this->error('请输出数字');
            }
        }else{
            $this->error('警告此操作非超级管理员，已经记录操作记录，请勿再次尝试');
        }
    }

    /*
     * 用户资金记录
     */
    Public function cost_log_Index() {
        $LS = Service('M', 'cost_log');
        $order = 'CREATE_TIME desc';
        $_GET['TYPE'] ? $where = array('MONEY_TYPE' => $_GET['TYPE']) : $where = array('MONEY_TYPE' => 1);

        $COST_LOG_LABLES = C('COST_LOG_LABLE');
        $this->COST_LOG_LABLE = $COST_LOG_LABLES[$where['MONEY_TYPE']];
        $this->USER_NAME = $_GET['USER_NAME'];
        $this->PAY_TYPE = C('PAY_TYPE');

        $count = $LS->count($where);
        $Page = new \Think\NewPage($count, 10);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $this->show = $show;
        $DATA = $LS->query($where, $limit, null, $order);
        $this->DATA = $DATA;

        $this->display();
    }


    /*
     * 初始化cost表
     */
    public function initCost() {
        $LS = Service('User', 'u_user');
        $DATA = $LS->query();
        $costDatas = array();
        foreach ($DATA as $k => $v) {
            $csave['USER_ID'] = $v['user_id'];
            $csave['BOOK_B'] = 0;
            $csave['MONEY'] = passport_encrypt('0.00', C('JM_KEY'));
            $csave['UPDATE_TIME'] = date('Y-m-d H:i:s');
            $csave['UPDATE_IP'] = get_client_ip();
            $costDatas[] = $csave;
        }
        $LS1 = Service('M', 'cost');
        $LS1->AddAll($costDatas);
    }
}